#include "OptionsState.h"
#include "stdafx.h"
/**************************************************************/
// GetInstance
//	- store the ONLY instance in global memory
//	- return the only instance by pointer
/*static*/ OptionsState* OptionsState::GetInstance(void)
{
	static OptionsState s_Instance;

	return &s_Instance;
}


/**************************************************************/
// Enter
//	- reset the game & load resources
void OptionsState::Enter(void)
{
	m_AOptionsVolume = SGD::AudioManager::GetInstance()->LoadAudio("resource/Sounds/OptionsVolume.wav");
	m_AOptionsVolume2 = SGD::AudioManager::GetInstance()->LoadAudio("resource/Sounds/OptionsVolume.xwm");
	m_hOptionsBar = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/Graphics/volumeBars.png");
	// Set background color
	SGD::GraphicsManager::GetInstance()->SetClearColor({ 50, 50, 50 });	// dark gray

}

/**************************************************************/
// Exit
//	- deallocate / unload resources
/*virtual*/ void OptionsState::Exit(void)
{
	SGD::AudioManager::GetInstance()->UnloadAudio(m_AOptionsVolume);
	SGD::AudioManager::GetInstance()->UnloadAudio(m_AOptionsVolume2);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hOptionsBar);
}

/**************************************************************/
// Input
//	- Handles User Input
bool OptionsState::Input()
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	if (Game::GetInstance()->MenuBackInput())
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());


	if (Game::GetInstance()->MenuConfirmInput())
	{
		if (m_nCursor == 2)
		{
			pAudio->PlayAudio(theGame->GetMenuConfirm());
			SGD::GraphicsManager::GetInstance()->Resize({ (float)Game::GetInstance()->GetScreenWidth(), (float)Game::GetInstance()->GetScreenHeight() }, Game::GetInstance()->GetWindowd());
			Game::GetInstance()->SetWindowd(!Game::GetInstance()->GetWindowd());
		}
		if (m_nCursor == 3)
		{
			pAudio->PlayAudio(theGame->GetMenuConfirm());
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
		}

		
	}

	// Move cursor
	if (Game::GetInstance()->MenuDownInput())
	{
		++m_nCursor;

		// Wrap around
		if (m_nCursor > 3)		// should enumerate the options
			m_nCursor = 0;
	}
	else if (Game::GetInstance()->MenuUpInput())
	{
		--m_nCursor;

		// Wrap around
		if (m_nCursor < 0)
			m_nCursor = 3;
	}

	if (Game::GetInstance()->MenuLeftInput())
	{
		if (m_nCursor == 0)
		{
			if (Game::GetInstance()->GetMusicVol() > 0)
			{
				int music = Game::GetInstance()->GetMusicVol();
				Game::GetInstance()->SetMusicVol(music - 5);
				SGD::AudioManager::GetInstance()->SetMasterVolume(SGD::AudioGroup::Music, Game::GetInstance()->GetMusicVol());
				//SGD::AudioManager::GetInstance()->PlayAudio(m_AOptionsVolume2);

			}
		}
		if (m_nCursor == 1)
		{
			if (Game::GetInstance()->GetSoundVol() > 0)
			{
				int sounds = Game::GetInstance()->GetSoundVol();
				Game::GetInstance()->SetSoundVol(sounds - 5);
				SGD::AudioManager::GetInstance()->SetMasterVolume(SGD::AudioGroup::SoundEffects, Game::GetInstance()->GetSoundVol());
				SGD::AudioManager::GetInstance()->PlayAudio(m_AOptionsVolume);

			}
		}
	}
	if (Game::GetInstance()->MenuRightInput())
	{
		if (m_nCursor == 0)
		{
			if (Game::GetInstance()->GetMusicVol() < 100)
				Game::GetInstance()->SetMusicVol(Game::GetInstance()->GetMusicVol() + 5);
			SGD::AudioManager::GetInstance()->SetMasterVolume(SGD::AudioGroup::Music, Game::GetInstance()->GetMusicVol());
			//SGD::AudioManager::GetInstance()->PlayAudio(m_AOptionsVolume2);

		}
		if (m_nCursor == 1)
		{
			if (Game::GetInstance()->GetSoundVol() < 100)
				Game::GetInstance()->SetSoundVol(Game::GetInstance()->GetSoundVol() + 5);
			SGD::AudioManager::GetInstance()->SetMasterVolume(SGD::AudioGroup::SoundEffects, Game::GetInstance()->GetSoundVol());
			SGD::AudioManager::GetInstance()->PlayAudio(m_AOptionsVolume);

		}
	}

	return true;
}
/**************************************************************/
// Update
//	- update Objects / animations
void OptionsState::Update(float elapsedTime)
{
}
/**************************************************************/
// Render
//	- Render Objects / animations
void OptionsState::Render()
{
	// Use the game's font
	BitmapFont* pFont = Game::GetInstance()->GetFont();

	// Align text based on window width
	int width = (int)Game::GetInstance()->GetScreenWidth();
	// Display the game title centered at 4x scale
	pFont->Draw("OPTIONS", (int)((width - (4 * 32 * 4.0f)) / 2), 50, 1.0f, {});

	SGD::GraphicsManager * pGraphics = SGD::GraphicsManager::GetInstance();

	pGraphics->DrawTexture(theGame->GetMenuBackground(), { 0, 0 });

	float sound = float(Game::GetInstance()->GetSoundVol());
	float music = float(Game::GetInstance()->GetMusicVol());

	pFont->Draw("Music Volume", 400, 85, 0.7f, {});
	pGraphics->DrawTextureSection(m_hOptionsBar, {325,150}, {17,41,434,63});
	pGraphics->DrawRectangle({ 325 + (music*4), 145, 340 + (music*4), 175 }, { 0, 0, 0, 0 }, {0,0,0},3);
//	pGraphics->DrawRectangle({ 325.0f, 150.0f, 350.0f + (music * 4.0f), 200.0f }, { 255, 0, 0 });
	pFont->Draw("Sound Volume", 400, 205, 0.7f, {});
	pGraphics->DrawTextureSection(m_hOptionsBar, { 325, 250 }, { 17, 41, 434, 63 });
	pGraphics->DrawRectangle({ 325 + (sound * 4), 245, 340 + (sound * 4), 275 }, { 0, 0, 0, 0 }, { 0, 0, 0 }, 3);
//pGraphics->DrawRectangle({ 325.0f, 250.0f, 350.0f + (sound * 4.0f), 300.0f }, { 255, 0, 0 });

	// Display the cursor next to the option
	SGD::GraphicsManager::GetInstance()->DrawTextureSection(theGame->GetCursorImage(), { 150, 100 + 120.0f * m_nCursor }, { 0, 0, 100, 35 });
	pFont->Draw("Exit(B)", 400, 450, 0.7f, {});
	pFont->Draw("FullScreen", 400, 330, 0.7f, {});
}